<template>
  <div class="wfull">
    <a-form :labelCol="{ span: 6 }" :wrapperCol="{ span: 18 }">
      <a-form-item label="标题" required>
        <ZgInput v-model="obj.title" placeholder="请输入标题" ></ZgInput>
      </a-form-item>
      <a-form-item label="发布者" required>
        <ZgInput v-model="obj.author" placeholder="请输入发布者"></ZgInput>
      </a-form-item>
      <a-form-item label="是否发布" required>
        <ZgSwitch v-model="obj.isPublish" :text="['是','否']" :width="60" ></ZgSwitch>
      </a-form-item>
      <a-form-item label="是否置顶" required>
        <ZgSwitch v-model="obj.isTop" :text="['是','否']" :width="60" ></ZgSwitch>
      </a-form-item>
      <a-form-item label="是否所有人可见" required>
        <ZgSwitch v-model="obj.isAll" :text="['是','否']" :width="60" ></ZgSwitch>
      </a-form-item>
      <a-form-item label="开始日期">
        <ZgDateTimePicker v-model="obj.stime"></ZgDateTimePicker>
      </a-form-item>
      <a-form-item label="结束日期">
        <ZgDateTimePicker v-model="obj.etime"></ZgDateTimePicker>
      </a-form-item>
      <a-form-item label="公告内容" required>
        <ZgTinymce v-model="obj.content" class="pos-rlt" style="top:-15px;"></ZgTinymce>
      </a-form-item>
      <a-form-item label="相关附件" required>
        <ZgUpFile v-model="fileArr"></ZgUpFile>
      </a-form-item>
    </a-form>
  </div>
</template>
<script>
export default {
  props: {
    jsonStr: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      fileArr: [],
      obj: {
        id: '',
        title: '',
        content: '',
        author: '',
        isTop: false,
        isPublish: false,
        isAll: false,
        stime: '',
        etime: ''
      }
    }
  },
  watch: {
    jsonStr () {
      this.init()
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      if (this.jsonStr && this.jsonStr.length > 0) {
        this.obj = JSON.parse(this.jsonStr)
        this.$http.get('/zg-system/ann/info', {
          annId: this.obj.id
        }, data => {
          this.obj.content = data[1].content
          this.fileArr = data[2].map(t => {
            return {
              name: t.fileName,
              size: t.fileSize,
              path: t.filePath
            }
          })
        })
      }
    },
    save () {
      if (this.obj.title.length === 0) {
        return this.$result.error('请输入公告标题')
      }
      if (this.obj.author.length === 0) {
        return this.$result.error('请输入发布者')
      }
      if (this.obj.content.length === 0) {
        return this.$result.error('请输入内容')
      }

      return this.$result.ok(Object.assign({
        fileVoList: this.fileArr.map(t => {
          return {
            name: t.name,
            size: t.size,
            path: t.path
          }
        })
      }, this.obj))
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
